package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.UserMessage;
import com.battle.pojo.param.userMessage.*;
import com.battle.pojo.vo.userMessage.*;
import com.battle.pojo.dto.userMessage.*;
import com.battle.service.repository.in.UserMessageRepository;
import com.battle.service.service.in.UserMessageService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：用户消息服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class UserMessageServiceImpl implements UserMessageService {

    private final UserMessageRepository userMessageRepository;

    public UserMessageServiceImpl(UserMessageRepository userMessageRepository) {
        this.userMessageRepository = userMessageRepository;
    }


    /**
     * 创建用户消息
     *
     * @param param
     */
    @Override
    public void createUserMessage(CreateUserMessageParam param) {
        UserMessage userMessage = new UserMessage();
        BeanUtils.copyProperties(param, userMessage);
        Long id = IdGenerator.SNOW.generate();
        userMessage.setId(id);
        userMessageRepository.create(userMessage);
    }

    /**
     * 批量已读消息
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editUserMessage(EditUserMessageParam param) {

        List<UserMessage> userMessages = userMessageRepository.listByIds(param.getIds());
        for (UserMessage userMessage : userMessages) {
            userMessage.setIsRead(true);
            userMessageRepository.edit(userMessage);
        }
    }

    /**
     * 移除用户消息
     *
     * @param id
     */
    @Override
    public void removeUserMessage(Long id) {
        userMessageRepository.removeById(id);
    }

    /**
     * 启动/禁用用户消息
     *
     * @param id
     */
    @Override
    public void enabledUserMessage(Long id) {
        UserMessage userMessage = userMessageRepository.getByIdCheck(id);
        userMessage.setIsDisable(!userMessage.getIsDisable());
        userMessageRepository.edit(userMessage);
    }

    /**
     * 获取用户消息详情
     *
     * @param id
     * @return
     */
    @Override
    public GetUserMessageVo getUserMessage(Long id) {
        UserMessage userMessage = userMessageRepository.getByIdCheck(id);
        GetUserMessageVo vo = new GetUserMessageVo();
        BeanUtils.copyProperties(userMessage, vo);
        return vo;
    }

    /**
     * 获取用户消息 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUserMessageListVo> getUserMessageList(GetUserMessageListParam param) {

        SelectUserMessageDto dto = new SelectUserMessageDto();
        BeanUtils.copyProperties(param, dto);
        Page<UserMessage> page = userMessageRepository.selectUserMessage(dto);
        List<UserMessage> userMessageList = page.getRecords();

        List<GetUserMessageListVo> list = new ArrayList<>();
        for (UserMessage userMessage : userMessageList) {
            GetUserMessageListVo vo = new GetUserMessageListVo();
            BeanUtils.copyProperties(userMessage, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取用户消息下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetUserMessageDownListVo> getUserMessageDownList(GetUserMessageDownListParam param) {
        UserMessage po = new UserMessage();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<UserMessage> userMessageList = userMessageRepository.getListByEntity(po);

        List<GetUserMessageDownListVo> list = new ArrayList<>();
        for (UserMessage userMessage : userMessageList) {
            GetUserMessageDownListVo vo = new GetUserMessageDownListVo();
            BeanUtils.copyProperties(userMessage, vo);
            list.add(vo);
        }
        return list;
    }

    /**
     * 获得未提交的消息数量
     * @param id
     * @return
     */
    @Override
    public Integer getUserMessageCount(Long id) {
        UserMessage po = new UserMessage();
        po.setUserId(id);
        po.setIsDelete(false);
        po.setIsRead(false);

        List<UserMessage> userMessageList = userMessageRepository.getListByEntity(po);
        if (CollectionUtil.isNotEmpty(userMessageList)){
            return userMessageList.size();
        }else{
            return 0;
        }
    }

}



